package cn.edu.nciae.libindex.controller;

import cn.edu.nciae.libindex.entity.Book;
import cn.edu.nciae.libindex.repository.BookRepo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Controller
 *
 * @author suddev
 * @create 2018-03-31 下午1:10
 **/
@RestController
@Slf4j
public class BookController {

    @Resource
    private BookRepo bookRepo;

    @GetMapping("/book/get/{name}")
    public List<Book> getBook(@PathVariable String name) {
        return bookRepo.findByBookName(name);
    }

    @PostMapping("/book/put")
    public String putBook(@RequestBody Book book) {
        if (book != null && StringUtils.isNotBlank(book.getBookName())) {
            log.info("收到数据:{}", book);
            return bookRepo.save(book).getId();
        }
        log.info("无效数据 PASS");
        return "无效数据PASS";
    }

    @GetMapping("/book/all")
    public List<Book> getAll() {
        return Lists.newArrayList(bookRepo.findAll());
    }

    @GetMapping("/book/clear")
    public String clear() {
        bookRepo.deleteAll();
        return "success";
    }
}
